package com.seteam.collabcrossword.windows;

import com.seteam.collabcrossword.workers.DefinitionFetcher;
import net.miginfocom.swing.MigLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutionException;

public class SearchBar extends JPanel {
    private final Logger logger = LoggerFactory.getLogger(SearchBar.class);
    private JTextField searchBox;
    private JButton searchButton;
    private Container container;
    private JTextArea resultBox;
    private JScrollPane scrollPane;
    private GroupLayout layout;
    private MigLayout migLayout;

    public SearchBar() {
        super();

        searchBox = new JTextField("Search for a word");
        searchButton = new JButton("Search");
        resultBox = new JTextArea();
        //layout = new GroupLayout(this);
        migLayout = new MigLayout("",                 // Layout Constraints
                "[grow][][grow]",   // Column constraints
                "[][shrink 1]");    // Row constraints ;
        init();
    }

    private void init() {
        this.setLayout(migLayout);
        scrollPane = new JScrollPane(resultBox);
        resultBox.setEditable(false);
        resultBox.setText("This is some sample text");
        //scrollPane.setSize(2, 20);
        searchButton.setEnabled(false);
        resultBox.setPreferredSize(new Dimension(800, 90));
        scrollPane.setPreferredSize(new Dimension(800, 90));
        searchBox.setPreferredSize(new Dimension(650, 30));

        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);


        /**layout.setAutoCreateGaps(true);
         layout.setAutoCreateContainerGaps(true);
         layout.setHorizontalGroup(layout.createSequentialGroup()
         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
         .addComponent(searchBox)
         .addComponent(scrollPane)
         )
         .addComponent(searchButton)
         );
         layout.setVerticalGroup(layout.createSequentialGroup()
         .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
         .addComponent(searchBox)
         .addComponent(searchButton))
         .addComponent(scrollPane));*/

        this.add(searchBox);
        this.add(searchButton, "wrap");
        this.add(scrollPane, "span");

        //If the search box contains text, the search button is enabled. Else it's disabled
        //This code handles it
        searchBox.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(final DocumentEvent documentEvent) {
                searchButton.setEnabled(true);
            }

            @Override
            public void removeUpdate(final DocumentEvent documentEvent) {
                if (searchBox.getText().length() == 0) {
                    searchButton.setEnabled(false);
                }
            }

            @Override
            public void changedUpdate(final DocumentEvent documentEvent) {
                //Do nothing
            }
        });

        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent actionEvent) {
                logger.info("Word to be searched for: {}", searchBox.getText());
                DefinitionFetcher definitionFetcher = new DefinitionFetcher(searchBox.getText()) {
                    @Override
                    protected void done() {
                        try {
                            final String meaning = get();
                            resultBox.setText(meaning);
                        } catch (final InterruptedException e) {
                            logger.debug("Exception: {}", e.getMessage());
                        } catch (final ExecutionException e) {
                            logger.debug("Exception: {}", e.getMessage());
                        }
                    }
                };
                definitionFetcher.execute();
            }
        });
        this.setPreferredSize(new Dimension(800, 150));
        this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
        //setVisible(true);
    }


}
